package com.trmq.web;

import com.alibaba.fastjson.JSON;
import com.trmq.mapper.TestMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/5  10:29
 * 描述:
 */
@RestController
public class TestWebController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TestMapper testMapper;

    /**
     * 作者: LCG
     * 日期: 2021/3/5  10:44
     * 描述: 跟新一个数据
     * @Param []
     * @Return boolean
     */
    @RequestMapping("toUpdate")
    public boolean toUpdate(@RequestBody Map<String,Object > map){

        int i = testMapper.updateUserData(map);

        rabbitTemplate.convertAndSend("redis-exchange","redis-key",JSON.toJSONString(map));

        return true;
    }
    
    /**
     * 作者: LCG
     * 日期: 2021/3/5  10:44
     * 描述: 从缓存中读取用户数据
     * @Param []
     * @Return java.util.List
     */
    @RequestMapping("getUserList")
    public List getUserList(){

        List<Object> userList = redisTemplate.opsForHash().values("userList");
        if(userList.size()==0){
            //到数据库获取
            List<Map<String, Object>> userList1 = testMapper.getUserList();
            if(userList1.size()>0){
                for (Map<String, Object> map : userList1) {
                    redisTemplate.opsForHash().put("userList",map.get("id").toString(),map);
                }
            }
            return userList1;
        }else{
            return userList;
        }

    }




}
